import { ref } from "vue";

/**
 * 使用组件的内部方法的代理函数
 *
 * @returns 返回一个元组，包含元素引用和代理函数
 */
export function useComponentFunction(args:object) {
  const elementRef = ref<HTMLElement>({} as HTMLElement);
  const exposeFunction = new Proxy(
    {},
    {
      get(_, key: string | symbol) {
        if (args.hasOwnProperty(key)) {
          return args[key as keyof typeof args];
        }
        return elementRef.value?.[key as keyof HTMLElement];
      },
      has(_, key: string | symbol) {
        return key in args || key in elementRef.value;
      },
    }
  );
  return [elementRef, exposeFunction];
}
